package com.xuexiang.templateproject.utils;

import static com.xuexiang.xui.XUI.getContext;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.xuexiang.templateproject.bean.QuestionBean;
import com.xuexiang.templateproject.db.DaoMaster;
import com.xuexiang.templateproject.db.DaoSession;
import com.xuexiang.templateproject.db.QuestionBeanDao;

import java.util.List;


public class DbUtils {


    private static DbUtils mDbUtils;
    private final Context context;
    /**
     * Helper
     */
    private DaoMaster.DevOpenHelper mHelper;//获取Helper对象
    /**
     * 数据库
     */
    private SQLiteDatabase db;
    /**
     * DaoMaster
     */
    private DaoMaster mDaoMaster;
    /**
     * DaoSession
     */
    private DaoSession mDaoSession;
    private final QuestionBeanDao questionBeanDao;
    /**
     * 获取单例
     */
    public static DbUtils getInstance(Context context){
        if(mDbUtils == null){
            synchronized (DbUtils.class){
                if(mDbUtils == null){
                    mDbUtils = new DbUtils(context);
                }
            }
        }
        return mDbUtils;
    }    /**
     * 获取单例
     */
    public static DbUtils getInstance(){
        if(mDbUtils == null){
            synchronized (DbUtils.class){
                if(mDbUtils == null){
                    mDbUtils = new DbUtils(getContext());
                }
            }
        }
        return mDbUtils;
    }
    /**
     * 初始化
     * @param context
     */
    public DbUtils(Context context) {
        this.context = context;
        mHelper = new DaoMaster.DevOpenHelper(context,"card.db", null);
        mDaoMaster =new DaoMaster(getWritableDatabase());
        mDaoSession = mDaoMaster.newSession();
        questionBeanDao = mDaoSession.getQuestionBeanDao();
    }
    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase(){
        if(mHelper == null){
            mHelper = new DaoMaster.DevOpenHelper(context,"course.db",null);
        }
        SQLiteDatabase db =mHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     * @return
     */
    private SQLiteDatabase getWritableDatabase(){
        if(mHelper == null){
            mHelper =new DaoMaster.DevOpenHelper(context,"course.db",null);
        }
        SQLiteDatabase db = mHelper.getWritableDatabase();
        return db;
    }
    
    public QuestionBean getQuestionById(Long key){
        return questionBeanDao.load(key);
    }

    public long insertOrReplaceQuestion(QuestionBean questionBean){
        return questionBeanDao.insertOrReplace(questionBean);
    }
    public void saveQuestion(QuestionBean questionBean){
        questionBeanDao.save(questionBean);
    }
    public List<QuestionBean> getQuestionHistory(){
        return questionBeanDao.queryBuilder()
                .orderDesc(QuestionBeanDao.Properties.UpdateTime)
                .list();
    }
//
    public QuestionBean getQuestionByQuestionId(String questionid){
        List<QuestionBean> questionBeanList =  questionBeanDao.queryBuilder()
                .where(QuestionBeanDao.Properties.QuestionId.eq(questionid))
                .limit(1).list();
        if(questionBeanList.size()>0) return questionBeanList.get(0);
        else  return null;
    }

    public void deleteAll(){
        questionBeanDao.deleteAll();
    }
}
